<?php

namespace App\Admin\Forms;

use App\Models\TongUser;
use App\Models\TongUserOpera;
use App\Models\TongUserProfit;
use Dcat\Admin\Admin;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\DB;

class RechargeRepeatMoney extends Form
{
    use LazyWidget;
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
         //dump($input);

        // return $this->response()->error('Your error message.');

        //TongUser::query()->where('id',$input['id'])->increment('repeat_money',$input['repeat_money']);

        DB::transaction(function () use($input) {
            $user_info = TongUser::query()->where('id',$input['id'])->first();
            $pre = $user_info['repeat_money'];

            TongUser::query()->where('id',$input['id'])->increment('repeat_money',$input['repeat_money']);
            //增加流水记录
            TongUserProfit::query()->create([
                'u_id'=>$input['id'],
                'money'=>$input['repeat_money'],
                'type'=>16,//系统后台充值
                'symbol'=>1,
                'status'=>1,
                'remark'=>'系统充值提货券到账'.$input['repeat_money'],
                'befor_money'=>$pre,
                'after_money'=>bcadd($pre,$input['repeat_money'],2),
            ]);

            //增加操作记录
            $admin_id = Admin::user()->id;

            TongUserOpera::query()->create([
                'admin_id'=>$admin_id,
                'type'=>2,
                'u_id'=>$input['id'],
                'desc'=>json_encode(['pre'=>$pre,'next'=>bcadd($pre,$input['repeat_money'],2)])
            ]);
        });

        return $this
				->response()
				->success('充值成功')
				->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->hidden('id','id')->required();
        $this->currency('repeat_money','充值金额')->required();
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        return [
            'id'=>$this->payload['id'] ?? null,
            'repeat_money'  => '',
        ];
    }
}
